﻿// Khai báo các namespace cần dùng 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyTuyenSinh.Base;
using QuanLyTuyenSinh.DataLayer;
using QuanLyTuyenSinh.Enum;

namespace QuanLyTuyenSinh.BussinessLayer
{
    public class UserLogin
    {
        public DataConfig DataConfig;
        private DataTable dtTable;

        public UserLogin()
        {
            DataConfig = new DataConfig(frmLoading.Servername, frmLoading.UserName, frmLoading.Pass);
        }

        public string UserId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public DateTime TimeCreate { get; set; }
        public string ActorCreate { get; set; }
        public int Status { get; set; }
        public DateTime TimeChanged { get; set; }
        public string ActorChanged { get; set; }

        public DataTable GetUserLoginByUserNameAndPass(UserLogin obj)
        {
            dtTable = new DataTable();
            const int memberStatus = (int) MemberStatus.Active;
            string sql = string.Format("SELECT * From UserLogin WHERE UserName=N'{0}' AND Password=N'{1}' AND Status={2}", obj.UserName, obj.Password, memberStatus);
            try
            {
                dtTable = DataConfig.GetDataTable(sql);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return dtTable;
        }

        public DataTable GetListUserLogin()
        {
            dtTable = new DataTable();
            string sql = string.Format("SELECT * From UserLogin");
            try
            {
                dtTable = DataConfig.GetDataTable(sql);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return dtTable;
        }

        public int Insert(UserLogin obj)
        {
            int result = 0;

            string sql = string.Format("INSERT INTO USERLOGIN values(N'{0}', N'{1}', N'{2}', '{3}', N'{4}', '{5}', N'{6}',N'{7}')"
                                       , obj.UserId, obj.UserName, obj.Password, obj.TimeCreate, obj.ActorCreate, obj.Status, obj.TimeChanged, obj.ActorChanged);
            try
            {
                result = DataConfig.ExecuteNoneQuery(sql);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return result;
        }

        public int Update(UserLogin obj)
        {
            int result = 0;
            string sql = string.Format("UPDATE USERLOGIN SET UserName=N'{0}', Password=N'{1}', TimeChanged='{2}', ActorChanged=N'{3}' " +
                                       "WHERE UserId=N'{4}'", obj.UserName, obj.Password, obj.TimeChanged, obj.ActorChanged, obj.UserId);
            try
            {
                result = DataConfig.ExecuteNoneQuery(sql);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return result;
        }

        public int UpdateStatus(UserLogin obj)
        {
            int result = 0;
            string sql = string.Format("UPDATE USERLOGIN SET Status='{0}' WHERE UserId=N'{1}'", obj.Status, obj.UserId);
            try
            {
                result = DataConfig.ExecuteNoneQuery(sql);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return result;
        }
    }
}